package com.cat.dynamicPlanning02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/custom-sort-string/
 * @create 2025/11/4 14:51
 * @since JDK17
 */

public class Solution29 {
    public String customSortString(String order, String s) {
        int[] cnt1 = new int[26], cnt2 = new int[26];
        char[] ans = new char[s.length()];
        for (char x : order.toCharArray()) {
            cnt1[x - 'a']++;
        }
        for (char x : s.toCharArray()) {
            cnt2[x - 'a']++;
        }
        int l = 0, r = s.length() - 1;
        for (char x : order.toCharArray()) {
            while (cnt2[x - 'a'] > 0) {
                ans[l++] = x;
                cnt2[x - 'a']--;
            }
        }
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < cnt2[i]; j++) {
                ans[l++] = (char) (i + 'a');
            }
        }
        return new String(ans);
    }
}
